//
//  VRHomeViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/11.
//

import UIKit

class VRHomeViewController: VRBaseViewController {

    private var topView = VRHomeTopView()
    private var hotCateView = VRHomeHotCateView()
    private var disCateView = VRHomeDiscoverCateView()
    private let hotColView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private let disColView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private let multiColView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    
    private var type = 1
    // 1.hot all 2.hot male 3.hot female 4.hot voice 
    //5.discover English 6.discover Español 7.discover Português 8.discover Deutsch 9.discover العربية"
    //10.discover Italiano 11.discover Le français 12.discover Filipino
    private var cateType = 1
    var dataArray:[InfoUtil.room] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            topView
            hotCateView
            disCateView
            hotColView
            disColView
            multiColView
        }
        
        disCateView.isHidden = true
        topView.leading(0).trailing(0).top(.statusHeight).height(44)
        hotCateView.leading(0).trailing(0).height(45).top(.statusHeight + 45)
        disCateView.leading(0).trailing(0).height(150).top(.statusHeight + 45)
        hotColView.leading(0).trailing(0).bottom(.tabBar_height)
        hotColView.Top == hotCateView.Bottom
        disColView.leading(0).trailing(0).bottom(.tabBar_height)
        disColView.Top == disCateView.Bottom
        multiColView.leading(0).trailing(0).bottom(.tabBar_height)
        multiColView.Top == topView.Bottom + 5
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 5
        layout.minimumInteritemSpacing = 5
        layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
        hotColView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(VRHomeHotCell.self, forCellWithReuseIdentifier: VRHomeHotCell.description())
            v.showsVerticalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
        }
        disColView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(VRHomeDisCell.self, forCellWithReuseIdentifier: VRHomeDisCell.description())
            v.showsVerticalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
            v.isHidden = true
        }
        multiColView.style { v in
            v.collectionViewLayout = layout
            v.backgroundColor = .clear
            v.register(VRHomeMultiCell.self, forCellWithReuseIdentifier: VRHomeMultiCell.description())
            v.showsVerticalScrollIndicator = false
            v.delegate = self
            v.dataSource = self
            v.isHidden = true
        }
        
        topView.hotBlock = { [weak self] in
            self?.hotCateView.isHidden = false
            self?.disCateView.isHidden = true
            self?.hotColView.isHidden = false
            self?.disColView.isHidden = true
        }
        topView.discoverBlock = { [weak self] in
            self?.hotCateView.isHidden = true
            self?.disCateView.isHidden = false
            self?.hotColView.isHidden = true
            self?.disColView.isHidden = false
        }
        topView.multiBlock = { [weak self] in
            self?.hotCateView.isHidden = true
            self?.disCateView.isHidden = true
            self?.hotColView.isHidden = true
            self?.disColView.isHidden = true
        }
        hotCateView.hotCateBlock = { type in
            self.cateType = type
        }
        disCateView.disCateBlock = { type in
            self.cateType = type
        }
        
        loadData()
        UserDefaultsManager.removeForKey(forKey: .currentPage)
    }
    
    private func loadData(){
        
        RT.getRoomList { list in
            for item in list{
                if item?.info4 == "___*VoiceRoom" && item?.info0 == "VoiceRoom___*Live" {
                    self.dataArray.append(item!)
                }
            }
            
            RT.blockListWith { response in
                for data in response
                {
                    self.dataArray.removeAll(where: {$0.id == data?.id})
                }
                self.dealDataSource()
             }
        }
    }
    
    private func dealDataSource(){
        
        if cateType == 2 {
            //male
            self.dataArray.removeAll(where: {$0.info9 == "1"})
        }else if cateType == 3 {
            //female
            self.dataArray.removeAll(where: {$0.info9 == "0"})
        }else if cateType == 4 {
            //voice
            self.dataArray.removeAll(where: {$0.info7 == "0"})
        }else if cateType == 5 {
            //English
            self.dataArray.removeAll(where: {$0.info5 != "English"})
        }else if cateType == 6 {
            //Español
            self.dataArray.removeAll(where: {$0.info5 != "Español"})
        }else if cateType == 7 {
            //Português
            self.dataArray.removeAll(where: {$0.info5 != "Português"})
        }else if cateType == 8 {
            //Deutsch
            self.dataArray.removeAll(where: {$0.info5 != "Deutsch"})
        }else if cateType == 9 {
            //العربية"
            self.dataArray.removeAll(where: {$0.info5 != "العربية"})
        }else if cateType == 10 {
            //Italiano
            self.dataArray.removeAll(where: {$0.info5 != "Italiano"})
        }else if cateType == 11 {
            //Le français
            self.dataArray.removeAll(where: {$0.info5 != "Le français"})
        }else if cateType == 12 {
            //Filipino
            self.dataArray.removeAll(where: {$0.info5 != "Filipino"})
        }
        
        if self.type == 1 {
            self.hotColView.reloadData()
        }else if self.type == 1 {
            self.disColView.reloadData()
        }else if self.type == 1 {
            self.multiColView.reloadData()
        }
    }
}

extension VRHomeViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if collectionView == hotColView {
            guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: VRHomeHotCell.description(), for: indexPath) as? VRHomeHotCell else { return UICollectionViewCell() }
            item.model = dataArray[indexPath.row]
            return item
        }else if collectionView == disColView {
            guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: VRHomeDisCell.description(), for: indexPath) as? VRHomeDisCell else { return UICollectionViewCell() }
            item.model = dataArray[indexPath.row]
            return item
        }
        guard let item = collectionView.dequeueReusableCell(withReuseIdentifier: VRHomeMultiCell.description(), for: indexPath) as? VRHomeMultiCell else { return UICollectionViewCell() }
        item.model = dataArray[indexPath.row]
        return item
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

        let detailVC = VRLiveRoomViewController()
        detailVC.dataModel = dataArray[indexPath.row]
        self.present(detailVC, animated: true)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        if collectionView == hotColView {
            
            let w = (CGFloat.screenWidth - 25.0) / 2
            let h = (230.0 * w)/175
            return CGSize(width: w, height: h)
        }else if collectionView == disColView {
            
            let w = (CGFloat.screenWidth - 30.0) / 3
            return CGSize(width: w, height: w)
        }
        
        let w = (CGFloat.screenWidth - 25.0) / 2
        let h = w + 40
        return CGSize(width: w, height: h)
    }
}
